<?php
namespace app\controllers;
/**
 * Created by PhpStorm.
 * @author LiShaoWei
 * @param Date : 2016/4/8
 * @param Time : 11:16
 * @since 1.0
 */
use app\models\Book;
use app\models\BookArticle;
use app\models\ContactForm;
use app\models\UserOld;
use PhpAmqpLib\Channel\AMQPChannel;
use PhpAmqpLib\Connection\AMQPConnection;
use yii\filters\AccessControl;
use app\models\LoginForm;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\console\Exception;
use yii\web\NotFoundHttpException;
class AppsController extends Controller
{

    public $layout=false;
    public $enableCsrfValidation = false;
    //首页
    public function actionIndex()
    {
       if( \Yii::$app->request->isPost){
           $account=\Yii::$app->request->post('account');
           $password=\Yii::$app->request->post('password');
           if($account && $password){

           }else {
               throw new NotFoundHttpException('密码错误');
           }
       }else{
           return $this->render('index');
       }

       // throw new NotFoundHttpException('错误的请求');

    }

    public function actionAppss()
    {
        return $this->render('apps');
    }
    public function actionAppspost($r)
    {
        $book=new BookArticle();
        $bookmODEL=new Book();
        $id=$bookmODEL::find()->select('id')->where(['title'=>$r])->one();
        $model=$book::find()->select(['id','title'])->where(['book_id'=>$id])->asArray()->all();
        return $this->render('appslist',['model'=>$model]);
    }
    public function actionWords($s)
    {
        $book=new BookArticle();
        $model=$book::find()->where(['id'=>$s])->asArray()->one();
        return $this->render('appswords',['model'=>$model]);
    }
    public function actionApps()
    {
        $words=\Yii::$app->request->get('kewds');
        $words= urlencode($words);
        $heds=array('Accept:*/*',
            'Accept-Encoding:gzip, deflate, sdch',
            'Accept-Language:zh-CN,zh;q=0.8',
            'Connection:keep-alive',
            'Host:maps.google.cn',
            'Referer:http://www.earthol.com/',
            'User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36',
            'X-Client-Data:CKK2yQEIxLbJAQj9lcoB'
);
       $url="http://maps.google.cn/maps/api/js/GeocodeService.Search?4s$words&7sCN&9szh-CN&callback=_xdc_._g5lwws&token=120883";
//        $url="http://maps.google.cn/maps/api/js/GeocodeService.Search?4s$words&7sCN&9szh-CN&callback=_xdc_._cp4dna&token=".rand(10,20000);
        $curl=new \curl();
        return $curl->curl_get($url,$heds);
    }

    public function curl_get ( $url , $cookie_path = NULL , $is_redirect = FALSE , $refer = NULL , $proxy = NULL , $timeout = NULL )
    {
        $ch = curl_init ( $url );

        if (is_null ( $timeout )) {
            $timeout = 30;
        }
        curl_setopt ( $ch , CURLOPT_TIMEOUT , $timeout );
        if (!is_null ( $cookie_path )) {
            curl_setopt ( $ch , CURLOPT_COOKIEFILE , $cookie_path );
            curl_setopt ( $ch , CURLOPT_COOKIEJAR , $cookie_path );
        }
        if ($is_redirect) {
            curl_setopt ( $ch , CURLOPT_FOLLOWLOCATION , 1 );    //是否抓取跳转后的页面
        }
        if (!is_null ( $refer )) {
            curl_setopt ( $ch , CURLOPT_REFERER , $refer );
        }
        if (!is_null ( $proxy )) {
            curl_setopt ( $ch , CURLOPT_HTTPPROXYTUNNEL , true );
            curl_setopt ( $ch , CURLOPT_PROXY , $proxy );
            curl_setopt ( $ch , CURLOPT_PROXYTYPE , CURLPROXY_HTTP );
        }
        if ($proxy == 3) {
            curl_setopt ( $ch , CURLOPT_HEADER , 1 );
        }
        curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , true ); //获取数据返回
        curl_setopt ( $ch , CURLOPT_BINARYTRANSFER , true ); //在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回
        curl_setopt ( $ch , CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)" );
        curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , 1 ); // 获取的信息以文件流的形式返回
        $tmpInfo = curl_exec ( $ch ); // 执行操作
        if (curl_errno ( $ch )) {
            echo 'Errno' . curl_error ( $ch );
        }
        curl_close ( $ch ); // 关键CURL会话
        return $tmpInfo; // 返回数据
    }
    function vget ( $url , $ucookie )
    { // 模拟登录获取Cookie函数

        $curl = curl_init (); // 启动一个CURL会话
        curl_setopt ( $curl , CURLOPT_URL , $url ); // 要访问的地址
        curl_setopt ( $curl , CURLOPT_SSL_VERIFYPEER , 0 ); // 对认证证书来源的检查
        curl_setopt ( $curl , CURLOPT_SSL_VERIFYHOST , 2 ); // 从证书中检查SSL加密算法是否存在
        curl_setopt ( $curl , CURLOPT_USERAGENT , $_SERVER['HTTP_USER_AGENT'] ); // 模拟用户使用的浏览器
        curl_setopt ( $curl , CURLOPT_FOLLOWLOCATION , 1 ); // 使用自动跳转
        curl_setopt ( $curl , CURLOPT_AUTOREFERER , 1 ); // 自动设置Referer
        curl_setopt ( $curl , CURLOPT_COOKIEJAR , $ucookie ); // 存放Cookie信息的文件名称
        curl_setopt ( $curl , CURLOPT_COOKIEFILE , $ucookie ); // 读取上面所储存的Cookie信息
        curl_setopt ( $curl , CURLOPT_TIMEOUT , 30 ); // 设置超时限制防止死循环
        curl_setopt ( $curl , CURLOPT_HEADER , 0 ); // 显示返回的Header区域内容
        curl_setopt ( $curl , CURLOPT_RETURNTRANSFER , 1 ); // 获取的信息以文件流的形式返回
        $tmpInfo = curl_exec ( $curl ); // 执行操作
        if (curl_errno ( $curl )) {
            echo 'Errno' . curl_error ( $curl );
        }
        curl_close ( $curl ); // 关闭CURL会话
        return $tmpInfo; // 返回数据
    }
    public function actionTest1()
    {

        $words=urldecode(\Yii::$app->request->get('kwords'));
        $url="http://api.map.baidu.com/?qt=gc&wd=$words&cn=%E5%8C%97%E4%BA%AC%E5%B8%82&ie=utf-8&oue=1&fromproduct=jsapi&res=api&callback=BMap._rd._cbk68399";
        $curl=new \curl();
        return $curl->curl_get($url);
    }
    public function actionTest2()
    {
        $words=urldecode(\Yii::$app->request->get('kwords'));
        $url="http://map.baidu.com/?newmap=1&reqflag=pcmap&biz=1&from=webmap&da_par=direct&pcevaname=pc4.1&qt=s&da_src=searchBox.button&wd=$words&c=289&src=0&wd2=&sug=0&l=19&b=(13540545.530000001,3639863.865;13540634.030000001,3640184.865)&from=webmap&biz_forward={%22scaler%22:1,%22styles%22:%22pl%22}&sug_forward=&tn=B_NORMAL_MAP&nn=0&u_loc=12967516.87792,4838534.533082&ie=utf-8&t=1467857316419";
        $curl=new \curl();
        $returnPost= json_decode($curl->curl_get($url),true);
        var_dump($returnPost['content'][0]['address_norm']);
    }

    function getDistance($lat1, $lng1, $lat2, $lng2)
    {
        $earthRadius = 6367000; //approximate radius of earth in meters


        $lat1 = ($lat1 * pi() ) / 180;
        $lng1 = ($lng1 * pi() ) / 180;

        $lat2 = ($lat2 * pi() ) / 180;
        $lng2 = ($lng2 * pi() ) / 180;


        $calcLongitude = $lng2 - $lng1;
        $calcLatitude = $lat2 - $lat1;
        $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
        $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
        $calculatedDistance = $earthRadius * $stepTwo;

        return round($calculatedDistance);
    }
    //注册
//    public function actionLogin()
//    {
//        $LoginFormModel=new LoginForm();
//        return $this->render('login',['model'=>$LoginFormModel]);
//
//    }
//    public function actionRegister()
//    {
//        return $this->render('register');
//    }
//    public function actionContent()
//    {
//        $ContactFormModel= new ContactForm();
//        return $this->render('content',['model'=>$ContactFormModel]);
//    }
}